﻿/// <reference path="General.js" />
/// <reference path="Browser.js" />
/// <reference path="Strings.js" />

eval("var Element = function() { }");

//debugger;

Element.saveDocGetById = document.getElementById;
Element.saveDocGetByName = document.getElementsByName;
Element.saveDocGetByTagName = document.getElementsByTagName;
Element.saveDocGetByClassName = document.getElementsByClassName;
Element.saveDocCreateElement = document.createElement;
Element.applyAllProperties = function applyAllProperties(element)
{
	if (element == null) return null;
	if (element._applyAllPropertiesDone || element.nodeName == "#text")
	{
		return element;
	}
	for (var i in Element.prototype)
		element[i] = Element.prototype[i];
	element._applyAllPropertiesDone = true;

	return element;
	return new Element();
}
Element.applyAllPropertiesArray = function applyAllPropertiesArray(elementArray)
{
	if (elementArray == null) return null;
	for (var i = 0; i < elementArray.length; ++i)
	{
		Element.applyAllProperties(elementArray[i]);
	}
	return elementArray;
	return new Array();
}
document.getElementById = function getElementById(id)
{
	return Element.applyAllProperties(Element.saveDocGetById(id));
	return new Element();
}
document.getElementsByName = function getElementsByName(name)
{
	return Element.applyAllPropertiesArray(Element.saveDocGetByName(name)); ;
	return new Array();
}
document.getElementsByTagName = function getElementsByTagName(tagName)
{
	return Element.applyAllPropertiesArray(Element.saveDocGetByTagName(tagName));
}
Element.prototype.getElementsByTagName = function getElementsByTagName_Element(tagName)
{
	var t = Element.saveDocGetByTagName(tagName);
	var res = new Array();
	for (var i = 0; i < t.length; ++i)
	{
		var c = t[i];

		while (c.parentNode != null && c.parentNode != this)
		{
			c = c.parentNode;
		}
		if (c.parentNode == this)
		{
			// IE workaround - when searching for any TABLE sub-element tags, it returns the TABLE itself instead
			if (c.tagName == "TABLE" && (tagName == "TBODY" || tagName == "THEAD" || tagName == "TFOOT"))
			{
				for (var iChild = 0; iChild < c.childNodes.length; ++iChild)
				{
					var childElem = c.childNodes[iChild];
					if (childElem.tagName == tagName)
					{
						res.push(childElem);
					}
				}
			}
			else
			{
				res.push(c);
			}
		}
	}
	return Element.applyAllPropertiesArray(res);
}
if (document.getElementsByClassName != null)
{
	document.getElementsByClassName = function getElementsByClassName(className)
	{
		return Element.applyAllPropertiesArray(Element.saveDocGetByClassName(className));
	}
	Element.prototype.getElementsByClassName = function getElementsByTagName_Element(className)
	{
		var t = Element.saveDocGetByClassName(className);
		var res = new Array();
		for (var i = 0; i < t.length; ++i)
		{
			var c = t[i];

			while (c.parentNode != null && c.parentNode != this)
			{
				c = c.parentNode;
			}
			if (c.parentNode == this)
			{
				res.push(c);
			}
		}
		return Element.applyAllPropertiesArray(res);
	}
}
document.createElement = function createElement(tagName)
{
	return Element.applyAllProperties(Element.saveDocCreateElement(tagName));
	return new Element();
}
Element.prototype.get_ChildNodes = function get_ChildNodes()
{
	if (this._childrenPropertiesDone !== true)
		Element.applyAllPropertiesArray(this.childNodes);
	return this.childNodes;
}
Element.prototype.get_ParentNode = function get_ParentNode()
{
	return Element.applyAllProperties(this.parentNode);
	return new Element();
}

function $(id)
{
	if (typeof (id) == "string")
		return document.getElementById(id);
	if (id.tagName != null && Element.applyAllProperties != null) // an element that needs to be "fixed"
		return Element.applyAllProperties(id);
	return id;
}
if (typeof (Events) != "undefined" && typeof (Events._AddEventObject) != "undefined")
{
	Element.prototype.AddEvent = window.AddEvent = document.AddEvent = Events._AddEventObject;
	Element.prototype.RemoveEvent = window.RemoveEvent = document.RemoveEvent = Events._RemoveEventObject;
}
